﻿/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;

namespace DaveSexton.DocProject.Sandcastle.TopicManagement
{
	internal sealed class TopicPropertiesToolWindow : ToolWindow
	{
		#region Public Properties
		public static readonly Guid ToolWindowGuid = new Guid("{F342FC85-9CF9-4ac1-883E-FF78DDA9D98E}");
		public static readonly TopicPropertiesToolWindow Instance = new TopicPropertiesToolWindow();

		public override System.Windows.Forms.Control Control
		{
			get
			{
				return control;
			}
		}

		public override string Caption
		{
			get
			{
				return Resources.Text.TopicPropertiesToolWindowCaption;
			}
		}

		public override System.Drawing.Bitmap Image
		{
			get
			{
				return Resources.Images.TopicManagement_ToolWindowIcon;
			}
		}
		#endregion

		#region Private / Protected
		private readonly TopicPropertiesControl control = new TopicPropertiesControl();
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="TopicPropertiesToolWindow" /> class.
		/// </summary>
		private TopicPropertiesToolWindow()
			: base(ToolWindowGuid)
		{
		}
		#endregion

		#region Methods
		public void SyncWithTopicExplorer()
		{
			Window.Caption = Caption;

			control.Manager = TopicExplorer.CurrentManager;

			Topic topic = TopicExplorer.CurrentTopic;

			if (topic == null || topic.IsApiPlaceholder)
				control.Topic = null;
			else
				control.Topic = topic;
		}
		#endregion

		#region Event Handlers
		protected override void OnShow(EventArgs e)
		{
			TopicExplorer.CurrentTopicChanged += TopicExplorer_CurrentTopicChanged;

			SyncWithTopicExplorer();

			base.OnShow(e);
		}

		protected override void OnHide(EventArgs e)
		{
			TopicExplorer.CurrentTopicChanged -= TopicExplorer_CurrentTopicChanged;

			if (control.Manager != null)
				control.Manager.SaveChanges(TopicManagerChanges.Properties);

			base.OnHide(e);
		}

		private void TopicExplorer_CurrentTopicChanged(object sender, EventArgs e)
		{
			SyncWithTopicExplorer();
		}
		#endregion
	}
}
